package org.spring.springboot.app.hard.hardregister.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.hard.hardregister.domain.po.HardRegisterPO;
import org.spring.springboot.app.hard.hardregister.domain.vo.*;
import org.spring.springboot.app.hard.hardregister.service.IHardRegisterService;
import org.spring.springboot.app.user.sysuser.domain.vo.SysUserAddVO;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.spring.springboot.util.DateUtils;
import org.spring.springboot.util.excel.ExportExcel;
import org.spring.springboot.util.excel.ImportExcel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(description="设备注册操作接口",tags = ApiIndex.HardRegisterController)
@RequestMapping(value = "/api/hardRegister")
@RestController
public class HardRegisterController {
    @Autowired
    private IHardRegisterService hardRegisterService;

    @ApiOperation(value="查询设备注册信息列表")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<HardRegisterVO>>> findAll(
            @ApiParam(value = "设备编号", required = false) @RequestParam(defaultValue = "") String hardId,
            @ApiParam(value = "设备标签", required = false) @RequestParam(defaultValue = "") String hardSign,
            @ApiParam(value = "设备类型(多类型可以,分割)", required = false) @RequestParam(defaultValue = "") String hardType,
            @ApiParam(value = "排序字段(需要与数据库字段对应)", required = false) @RequestParam(defaultValue = "") String sortField,
            @ApiParam(value = "排序方式（ascend正序,descend倒序）", required = false) @RequestParam(defaultValue = "") String sortOrder,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize){

        HardRegisterSearchVO searchVo= new HardRegisterSearchVO();
        searchVo.setHardId(hardId);
        searchVo.setHardType(hardType);
        searchVo.setHardSign(hardSign);
        searchVo.setSortField(sortField);
        searchVo.setSortOrder(sortOrder);
        searchVo.setStartCreateDate(startCreateDate);
        searchVo.setEndCreateDate(endCreateDate);
        searchVo.setPageNum(pageNum);
        searchVo.setPageSize(pageSize);

        R<PageInfo<List<HardRegisterVO>>> result = new R();
        List<HardRegisterVO> _list = hardRegisterService.findAll(searchVo);
        PageInfo p = new PageInfo(_list);
        p.setList(_list);
        result.setData(p);
        return result;
    }



    @ApiOperation(value="根据ID得到设备注册信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    // @Cacheable(value="user")//用redis缓存方法返回结果,默认10秒钟
    public R<HardRegisterVO> find(@ApiParam(value = "注册Id") @RequestParam(defaultValue = "") String id) {
        R<HardRegisterVO> r = new R<HardRegisterVO>();
        HardRegisterPO po = hardRegisterService.find(id);
        HardRegisterVO vo = new HardRegisterVO();
        BeanUtils.copyProperties(po,vo);
        r.setData(vo);
        return r;
    }

    /**
     * 添加
     */
    @ApiOperation(value="添加设备注册信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "用户对象", required = true)@RequestBody HardRegisterAddVO vo,
            BindingResult bindingResult
    ) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        result = hardRegisterService.save(vo);
        return result;
    }

    /**
     * 修改
     */
    @ApiOperation(value="修改设备注册信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(@ApiParam(value = "用户对象", required = true) @RequestBody HardRegisterUpdateVO vo) {
        return hardRegisterService.update(vo);
    }


    /**
     * 删除
     * @param id
     */
    @ApiOperation(value="删除设备注册信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(@ApiParam(value = "Id(如果删除多个注册则以,分割)", required = true) @PathVariable("id") String id) {
        return hardRegisterService.delete(id);
    }


    @ApiOperation(value="导出设备注册Excel模板")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    public R exportExcel(
            @ApiParam(value = "request", hidden = true) HttpServletRequest request,
            @ApiParam(value = "response", hidden = true) HttpServletResponse response
    ) {
        R r = new R();

        String fileName = "设备注册Excel模板" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        try {
            ExportExcel e =  new ExportExcel("导入时的设备编号(必填)、设备标签(必填)、设备密码（必填）", ExcelHardRegisterVO.class).write(response, fileName).dispose();
        } catch (Exception e) {
            return r.fail().setMessage("导出设备注册Excel模板失败！失败信息:"+e.getMessage());
        }
        return r.success();
    }

    @ApiOperation(value="导出二维码地址Excel")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/exportUrlExcel", method = RequestMethod.GET)
    public R exportUrlExcel(
            @ApiParam(value = "request", hidden = true) HttpServletRequest request,
            @ApiParam(value = "response", hidden = true) HttpServletResponse response
    ) {
        R r = new R();
        //得到房间的数据
        List<ExcelHardRegisterUrlVO> list = hardRegisterService.findExcelAllList();
        String fileName = "二维码地址" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        try {
            ExportExcel e =  new ExportExcel("二维码地址", ExcelHardRegisterUrlVO.class).setDataList(list).write(response, fileName).dispose();
        } catch (Exception e) {
            return r.fail().setMessage("二维码地址！失败信息:"+e.getMessage());
        }
        return r.success();
    }



    @ApiOperation(value="导入设备注册")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public R importExcel(@ApiParam(value = "导入的Excel", required = true) @RequestParam("file") MultipartFile file) {
        R r = new R();
        if (file.isEmpty()) {
            return r.fail().setMessage("文件为空");
        }
        try {
            ImportExcel ei = new ImportExcel(file, 1, 0);
            r=hardRegisterService.opExcelHardRegistr(ei.getDataList(ExcelHardRegisterVO.class));
        } catch (Exception e) {
            return r.fail().setMessage("导出房间失败！失败信息:"+e.getMessage());
        }
        return r.success();
    }

}
